import { Component, OnInit, Inject } from '@angular/core';
import {ActivatedRoute} from '@angular/router';
import {CompanyService} from '../../service/company.service';
import {Position} from '../../entity/position';
import {AuthService} from '../../service/auth.service';
import {TalentService} from '../../service/talent.service';
import {Resume} from '../../entity/resume';
import {MatDialog, MatDialogRef, MAT_DIALOG_DATA} from '@angular/material';
import {Company} from '../../entity/company';

@Component({
  selector: 'app-position-details',
  templateUrl: './position-details.component.html',
  styleUrls: ['./position-details.component.css']
})
export class PositionDetailsComponent implements OnInit {
  resumes: any = new Array<Resume>();
  position: any = new Position();
  company: any = new Company();
  constructor(
    private route: ActivatedRoute,
    private companyService: CompanyService,
    public authService: AuthService,
    private  talentService: TalentService,
    public dialog: MatDialog
  ) {
    this.getPosition();
    this.loadData();
  }

  ngOnInit() {
  }

  getPosition() {
    const id = this.route.snapshot.paramMap.get('id');
    this.companyService.findPositonOne(Number.parseInt(id))
      .subscribe(position => {
        this.position = position;
        this.companyService.findCompanyInfo(this.position.companyId)
          .subscribe(company => this.company = company);
      });
  }

  loadData() {
    if (this.authService.isTalent) {
      let seq = this.talentService.findPositonList();
      seq.subscribe(resumes => this.resumes = resumes);
    }
  }

  openDialog() {
    const dialogRef = this.dialog.open(PositionDetailsDialogComponent, {
      // height: '350px'
      data: {resumes: this.resumes, position: this.position}
    });

    dialogRef.afterClosed().subscribe(result => {
      console.log(`Dialog result: ${result}`);
    });
  }

}

@Component({
  selector: 'app-position-details-dialog',
  templateUrl: './position-details-dialog.component.html',
  styleUrls: ['./position-details-dialog.component.css']
})
export class PositionDetailsDialogComponent {
  resumeId: any = -1;
  constructor(
    public dialogRef: MatDialogRef<PositionDetailsDialogComponent>,
    @Inject(MAT_DIALOG_DATA) public data: any,
    private  talentService: TalentService,
    private route: ActivatedRoute,
  ) { }
  closeDialog() {
    this.dialogRef.close('Pizza!');
  }
  checkResume(resumeId: number) {
    this.resumeId = resumeId;
    console.log(this.resumeId);
  }

  apply() {
    if (this.resumeId >= 0) {
      this.talentService.apply(this.data.position.id, this.resumeId);
    }
  }
}
